<?php

namespace app\index\controller;
//搜索
class Search extends Base
{

    /**
     * 军区定制
     */
    public function indexSS($navid = 'null')
    {
        if ($this->request->isGet()) {
            $table = $this->request->param('model');
            if ($table) {
                $list = db($table)
                    ->where('status', '1')
                    ->where('lang', cookie('think_var'))
                    ->where('title', 'like', '%' . $this->request->param('title') . '%')
                    ->paginate()
                    ->appends($this->request->param());
                $this->assign('cateInfo', '');
                $this->assign('model', $table);

                /*栏目详情end*/
            } else {
                $list['article'] = db('article')
                    ->where('status', '1')
                    ->where('lang', cookie('think_var'))
                    ->where('title', 'like', '%' . $this->request->param('title') . '%')
                    ->limit(10)
                    ->select();
                $list['bbs'] = db('bbs')
                    ->where('status', '1')
                    ->where('title', 'like', '%' . $this->request->param('title') . '%')
                    ->limit(10)
                    ->select();
                $list['download'] = db('download')
                    ->where('status', '1')
                    ->where('title', 'like', '%' . $this->request->param('title') . '%')
                    ->limit(10)
                    ->select();
                $list['research_copy'] = db('research_copy')
                    ->where('status', '1')
                    ->where('title', 'like', '%' . $this->request->param('title') . '%')
                    ->limit(10)
                    ->select();
            }
        }
//            dump($list);
//exit();

        $this->assign('list', $list);
        $this->assign('empty', lang('No relevant data'));
        /*列表end*/
        return $this->fetch($this->template . 'search.html');

    }

    /**
     * 默认搜索
     * @param string $navid
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index($navid = 'null')
    {
        if ($this->request->isGet()) {
            $table['model'] = $this->request->param('model');
            $table['lang'] = cookie('think_var');
            $cateInfo = db('nav')->where($table)->find();
            $list ='';
            if (empty($cateInfo)) {
                /*栏目详情end*/
//                $this->redirect($this->request->domain());
//                $this->error('栏目不存在');
            }else {
                switch ($cateInfo['model']) {
                    case 'Supply':
                    case 'Purchase':
                        /*列表start*/
                        $list = db($cateInfo['model'])
                            ->where('status', '1')
                            ->where('lang', cookie('think_var'))
                            ->where('title', 'like', '%' . $this->request->param('title') . '%')
                            ->paginate()
                            ->appends($this->request->param());
                        break;
                    default:
                        $list = db($cateInfo['model'])->alias('a')
                            ->join('__NAV__ c', 'c.id= a.cid')
                            ->field('a.*,c.name')
                            ->where('c.status = a.status')
                            ->where('a.status', '1')
                            ->where('a.lang = c.lang')
                            ->where('a.lang', cookie('think_var'))
                            ->where('a.title', 'like', '%' . $this->request->param('title') . '%')
                            ->paginate()
                            ->appends($this->request->param());
                        break;

                }
                /*
                            if($cateInfo['model']=='Supply'||$cateInfo['model']=='Purchase'){
                                //列表start
                                $list = db($table)
                                    ->where('status', '1')
                                    ->where('lang',cookie('think_var'))
                                    ->where('title', 'like', '%' . $this->request->param('title') . '%')
                                    ->paginate()
                                    ->appends($this->request->param());
                            }else{
                                $list = db($table)->alias('a')
                                    ->join('__NAV__ c', 'c.id= a.cid')
                                    ->field('a.*,c.name')
                                    ->where('c.status = a.status')
                                    ->where('a.status', '1')
                                    ->where('a.lang = c.lang')
                                    ->where('a.lang',cookie('think_var'))
                                    ->where('a.title', 'like', '%' . $this->request->param('title') . '%')
                                    ->paginate()
                                    ->appends($this->request->param());
                //                $list = db($table)
                //                    ->where('status', '1')
                //                    ->where('lang',cookie('think_var'))
                //                    ->where('title', 'like', '%' . $this->request->param('title') . '%')
                //                    ->paginate()
                //                    ->appends($this->request->param());
                            }
                */

//            dump($list);
            }

            /*栏目详情end*/
            $this->assign('cateInfo', $cateInfo);
            $this->assign('empty', lang('No relevant data'));
            $this->assign('list', $list);
            /*列表end*/
            return $this->fetch($this->template . 'search.html');
        }

    }

}
